package com.yanu.trace.t2s.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanu.trace.t2s.entity.User;
import com.yanu.trace.t2s.mapper.UserMapper;
import com.yanu.trace.t2s.service.UserService;
import com.yanu.trace.t2s.util.JWTUtil;
import com.yanu.trace.t2s.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional //声明式事务
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();//获取所有用户
    }

    @Override
    public User findByUserid(int userid) {
        return userMapper.findByUserid(userid);//根据ID查询用户
    }

    @Override
    public List<User> findByIDL(int userid, String username) {
        username = ((username!=null)&&(username.trim().length() > 0))?"%"+username+"%":"";
        return userMapper.findByIDL(userid,username);//根据用户ID模糊查询
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);//添加用户
    }

    @Override
    public IPage<User> selectPage(Page<?> page, String statename,String statement) {
        return userMapper.selectPageVo(page);//对所有用户进行分页查询
    }

    @Override
    public Message register(User user) {
        Message msg = new Message();
        try {
            if(user.getEnter_id()==0){//不填写邀请码的情况下注册默认给予管理员
                user.setType(1);
            }else {
                user.setType(2);
            }
            QueryWrapper<User> wrapper = new QueryWrapper();
            wrapper.eq("username", user.getUsername())
                    .or().eq("telephone", user.getTelephone())
                    .or().eq("email",user.getEmail());//查询用户名手机或邮箱是否已经被使用
            int count = userMapper.selectCount(wrapper);//获取结果数目
            if (count != 0) {//已经存在
                msg.setMsg("用户名 电话号或邮箱已经存在");
                msg.setCode(401);
                return msg;
            }
            userMapper.addUser(user);//添加用户
            msg.setMsg("注册成功");
            msg.setCode(200);

        } catch (Exception e) {
            msg.setMsg("注册失败,未知错误");
            msg.setCode(499);
            throw e;
        }
        return msg;
    }

    @Override
    public Message login(String username, String password) {
        Message msg = new Message();
        QueryWrapper<User> wrapper = new QueryWrapper();//实例化查询包装器
        wrapper.eq("username", username).eq("password", password);//将前端传来的用户名与密码对数据库进行比对
        User user = userMapper.selectOne(wrapper);//取第一个符合条件的对象
        if (user == null) {//未查询到
            msg.setMsg("用户名或密码不正确");
            msg.setCode(402);
        } else {
            msg.setMsg("登陆成功");
            msg.setCode(200);
            msg.setObj(JWTUtil.jwtCreate(user.getUsername()));//传回Token
        }
        return msg;
    }

    @Override
    public User getinfo(String token) {
        User user = userMapper.getallinfo(JWTUtil.getUsername(token));//根据Token获取用户信息
        return user;
    }
}
